package com.imooc.security.demo.controller;

import com.imooc.security.browser.config.BrowserSecurityConfig;
import com.imooc.security.demo.config.WebConfig;
import com.imooc.security.demo.pojo.User;
import com.imooc.security.browser.config.MyUserDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 * @author JackYang
 * @description:
 * @create: 2020/6/13 17:51
 */
@Api(description ="测试一些基本的restful请求")
@org.springframework.web.bind.annotation.RestController
@RequestMapping("/rest")
public class RestController {
    
    @Autowired
    private WebConfig webConfig;

    @GetMapping("/hello")
    @ApiImplicitParam(name = "username", value = "yonghuming", paramType = "query", required = true, dataType = "String")
    public String hello(@ApiParam(name ="用户名称",value = "传入的用户名") @RequestParam(name = "username") String username){
        System.out.println("执行controller中的 hello 方法.........");
        handle();
        //throw new UserNotExistException("lallala....");
        //throw new RuntimeException("user is not exists....");
        return "hello world...:"+username;
    }
    
    public void handle(){
        System.out.println("正在处理中..........");
    }
    
    @PostMapping("/user")
    public User user(@Valid User user, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            bindingResult.getAllErrors().stream().forEach(error-> System.out.println(error.getDefaultMessage()));
        }
        System.out.println(user);
        return user;
    }
    
    @GetMapping("/userInfo")
    public User user(){
        //System.out.println(myUserDetailsService);
        User user = new User();
        user.setBirthday(new Date());
        user.setId("001");
        user.setUsername("zhangsan");
        user.setPassword("123456");
        return user;
    }
    
    
    @GetMapping("/query3/{id}")
    public String query3_1(@PathVariable String id){
        return "@GetMapping........."+id;
    }
    @PostMapping("/query3/{id}")
    public String query3_2(@PathVariable String id){
        return "@PostMapping........."+id;
    }
    @PutMapping("/query3/{id}")
    public String query3_3(@PathVariable String id){
        return "@PutMapping........."+id;
    }
    
    @DeleteMapping("/query3")
    public String query3_4(){
        return "@DeleteMapping.........";
    }
}
